#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"
. "$(dirname -- "$0")/common.sh"

cd generator/konfig-dash

# This hook is run before git push to wake up the changeset bot
echo "Sending HTTP GET request to changeset bot..."
while read local_ref local_sha remote_ref remote_sha
do
    branch_name=$(echo $remote_ref | sed -E 's/^refs\/heads\///')
    if [ "$branch_name" != "main" ]; then
        # suppress output to make curl silent
        (
            curl -s -X GET "https://changeset-bot.konfigthis.com/api/webhook" > /dev/null
            echo "Request to changeset bot has finished."
        ) &
    fi
done


# if NO_RELEASE is set, skip this hook
if [ -n "$NO_RELEASE" ]; then
  exit 0
fi

# run "yarn changeset status --since=main" but don't exit if fails, instead capture the exit code in a variable
yarn changeset status --since=main || exit_code=$?

# If last exit code is not 0, print a message saying that you can also prefix your commit with NO_RELEASE=1 to skip this hook
if [ $exit_code -ne 0 ]; then
  # add directions to cd into "generator/konfig-dash" and run "yarn changeset" to add a changeset
  echo "\033[0;31m ERROR: You need to add a changeset before pushing. \033[0m"
  echo "\033[0;31m ERROR: Run 'cd generator/konfig-dash && yarn changeset' to add a changeset. \033[0m"

  # add a little warning sign in a red bordered box
  echo "\033[0;31m WARNING: You can also prefix your commit with NO_RELEASE=1 if this change doesn't need a release \033[0m"
  exit 1
fi
